import { defineStore } from 'pinia'
import { computed, ref } from 'vue'

// 账号模块
export const useUserStore = defineStore(
  'big-user',
  () => {
    const user = ref({
      id: null,
      email: '',
      token: '',
      role: '',
      useSpace: 0,
      totalSpace: 0
    })

    // 计算属性方便访问单个值
    const id = computed(() => user.value.id)
    const role = computed(() => user.value.role)
    const token = computed(() => user.value.token)
    const email = computed(() => user.value.email)
    const useSpace = computed(() => user.value.useSpace)
    const totalSpace = computed(() => user.value.totalSpace)

    const setToken = data => {
      user.value.token = data
    }
    const setId = data => {
      user.value.id = data
    }
    const setEmail = data => {
      user.value.email = data
    }
    const setRole = data => {
      user.value.role = data
    }
    const setUseSpace = data => {
      user.value.useSpace = data
    }
    const setTotalSpace = data => {
      user.value.totalSpace = data
    }

    // 清除用户信息
    const clear = () => {
      user.value = {
        id: null,
        email: '',
        token: '',
        role: '',
        useSpace: 0,
        totalSpace: 0
      }
    }

    return {
      user,
      id,
      role,
      token,
      email,
      useSpace,
      totalSpace,
      setId,
      setToken,
      setEmail,
      setRole,
      setUseSpace,
      setTotalSpace,
      clear
    }
  },
  {
    // 持久化
    persist: true
  }
)
